<template>
	<el-dialog :title="`${titleMap[mode]}菜品信息:`" v-model="visible" width="70vw" height="75vh" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode === 'show'" ref="dialogForm" label-width="80px"
						 label-position="top" v-loading="category.loading" >
			<el-form-item label="类型：" prop="categoryId">
				<el-select v-model="form.categoryId" placeholder="请选择类型">
					<el-option
						v-for="item in category.dataList" :key="item.id" :label="item.name" :value="item.id"/>
				</el-select>
			</el-form-item>
			<el-form-item label="名称：" prop="name">
				<el-input v-model="form.name" clearable/>
			</el-form-item>
			<el-form-item label="单价：" type="number" prop="price">
				<el-input v-model="form.price" clearable/>
			</el-form-item>
			<el-form-item label="图片：" prop="thumbnail">
				<sc-upload v-model="form.thumbnail"/>
			</el-form-item>
			<el-form-item label="单点是否可配送：" prop="alone">
				<el-switch v-model="form.alone" />
			</el-form-item>
			<el-form-item label="赠送数量：" prop="free">
				<el-input v-model="form.free" type="number" clearable/>
			</el-form-item>
			<el-form-item label="菜品介绍：" prop="intro">
				<el-input v-model="form.intro" type="textarea" clearable/>
			</el-form-item>
			<el-form-item label="显示顺序：" prop="sort">
				<el-input-number v-model="form.sort" :min="1" :max="99"/>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button type="danger" :disabled="form.id === undefined" @click="onRemove">删除</el-button>
			<el-button v-if="mode !== 'show'" type="primary" :loading="saving" @click="onSubmit()">保存</el-button>
		</template>
	</el-dialog>
</template>
<script>
import {ElNotification} from 'element-plus'

export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "create",
			titleMap: {
				create: '新增',
				edit: '编辑',
				show: '查看'
			},
			saving: false,
			visible: false,
			form: {
				free: 0,
				sort: 99
			},
			rules: {
				name: [
					{required: true, message: '请输入名称'}
				],
				categoryId: [
					{required: true, message: '请选择类型'}
				],
				price: [
					{required: true, message: '请输入单价'}
				],
				thumbnail: [
					{required: true, message: '请上传菜品图片'}
				],
				free: [
					{required: true, message: '请输入免费赠送数量'}
				],
				sort: [
					{required: true, message: '显示顺序不能为空'}
				],
			},
			category: {
				loading: true,
				dataList: []
			}
		}
	},
	mounted() {
		this.$API.category.list.get().then(res => {
			this.category.loading = res.code !== 0;
			if (res.code === 0) this.category.dataList = res.data;
		})
	},
	methods: {
		open(mode = 'create') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		onSubmit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.saving = true;
					let res = {}
					if (this.mode === 'create') res = await this.$API.cate.create.post(this.form);
					else if (this.mode === 'edit') res = await this.$API.cate.detail.put(this.form.id, this.form);
					this.saving = false;
					if (res.code === 0) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		data(ele) {
			Object.assign(this.form, ele);
			return this;
		},
		onRemove() {
			this.$API.cate.detail.delete(this.form.id).then(res => {
				if (res.code === 0 && res.data) {
					ElNotification({
						message: '执行成功。',
						type: 'success',
					})
					this.visible = false;
					this.$emit('success', this.form.id)
				}
			})
		}
	}
}
</script>

<style lang="less" scoped>
.el-form {
	height: 45vh;
	overflow-y: auto;
	padding: 0 10px;
}
.el-select--default {
	width: 100%;
}
</style>
